package co.yixiang.modules.delay;

import co.yixiang.modules.auction.domain.YxAuctionOrder;
import co.yixiang.modules.auction.domain.YxAuctionRecord;
import co.yixiang.modules.auction.service.mapper.YxAuctionOrderMapper;
import co.yixiang.modules.auction.service.mapper.YxAuctionRecordMapper;
import co.yixiang.modules.commom.domain.YxAppdict;
import co.yixiang.modules.commom.service.mapper.YxAppdictMapper;
import co.yixiang.utils.RedisUtils;
import co.yixiang.utils.SpringApplicationContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 三十分钟未付款，删除订单
 */
@Component
public class DelayOrderWorker implements Runnable {


    @Override
    @Transactional
    public void run() {
        RedisUtils redisUtil= (RedisUtils) SpringApplicationContextHolder.getSpringBean("RedisUtils");
        YxAuctionOrderMapper yxAuctionOrderMapper= (YxAuctionOrderMapper) SpringApplicationContextHolder.getSpringBean("YxAuctionOrderMapper");
        YxAuctionRecordMapper yxAuctionRecordMapper=(YxAuctionRecordMapper)SpringApplicationContextHolder.getSpringBean("YxAuctionRecordMapper");
        YxAppdictMapper yxAppdictMapper=(YxAppdictMapper) SpringApplicationContextHolder.getSpringBean("YxAppdictMapper");
        //获取所有逾期付款订单id key
        List<String> keys = redisUtil.getLike("order");
        YxAppdict pmfksj=yxAppdictMapper.findAppdictByCode("pmfksj");
        Integer paimaiTime=60;
        if(null !=pmfksj){
            paimaiTime=Integer.parseInt(pmfksj.getMemo());
        }
        for (String key:keys) {
           String orderId =  redisUtil.get(key).toString();
            YxAuctionOrder yxAuctionOrder=yxAuctionOrderMapper.selectById(orderId);
            if(null !=yxAuctionOrder){
                Calendar instance = Calendar.getInstance();
                instance.setTime(yxAuctionOrder.getCreateTime());
                instance.add(Calendar.MINUTE,paimaiTime);
                Date newCreateTime = instance.getTime();
                //若为待付款
                if(0==yxAuctionOrder.getStatus()  && newCreateTime.compareTo(new Date())==-1){
                    //设为已取消
                    yxAuctionOrder.setStatus(5);
                    yxAuctionOrder.setIsDel(1);
                    YxAuctionRecord yxAuctionRecord=yxAuctionRecordMapper.selectById(yxAuctionOrder.getAuctionRecordId());
                    if(null !=yxAuctionRecord){
                        yxAuctionRecord.setIsBuy(0);
                        redisUtil.set("paimai"+yxAuctionRecord.getId(),yxAuctionRecord.getId());
                    }
                    yxAuctionOrderMapper.updateById(yxAuctionOrder);
                    yxAuctionRecordMapper.updateById(yxAuctionRecord);
                    redisUtil.del(key);
                }else if(0==yxAuctionOrder.getStatus()  && new Date().compareTo(newCreateTime)==-1){
                    DelayOrderWorker delayOrderWorker=new DelayOrderWorker();
                    DelayOrderQueueManager delayOrderQueueManager=DelayOrderQueueManager.getInstance();
                    delayOrderQueueManager.put(delayOrderWorker,(paimaiTime*60*1000+yxAuctionOrder.getCreateTime().getTime()-System.currentTimeMillis())/1000, TimeUnit.SECONDS);
                }
            }
        }
    }
}  